package com.github.llyb120.performance.define

class All{
    private final static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList()
        }
    }

    static Generator generator(Closure closure){
        def _generator = new Generator()
        pushStack(_generator)
        closure.call()
        return _generator
    }

    static Dataset dataset(Closure closure){
        def _dataset = new Dataset()
        pushStack(_dataset)
        closure.call()
        return _dataset
    }

    static Field field(Closure closure){
        def _field = new Field()
        def item = pickStack()
        if(item instanceof Dataset){
            item.getField().add(_field)
        }
        pushStack(_field)
        closure.call()
        popStack()
        return _field
    }

    static Field field(String en,String cn,String converter,Closure closure = null){
        def _field = new Field()
        _field.setEn(en)
        _field.setCn(cn)
        _field.setConverter(converter)
        def item = pickStack()
        if(item instanceof Dataset){
            item.getField().add(_field)
        }
        pushStack(_field)
        if(closure != null){
            closure.call()
        }
        popStack()
        return _field
    }

    static Converter converter(Closure closure){
        def _converter = new Converter()
        pushStack(_converter)
        closure.call()
        return _converter
    }

    static Scope scope(Closure closure){
        def _scope = new Scope()
        pushStack(_scope)
        closure.call()
        popStack()
        return _scope
    }

    static void name(String val){
        def item = pickStack()
        if(item instanceof Generator){
            item.setName(val)
        }
        if(item instanceof Converter){
            item.setName(val)
        }
        if(item instanceof Scope){
            item.setName(val)
        }
    }

    static void handler(Closure val){
        def item = pickStack()
        if(item instanceof Generator){
            item.setHandler(val)
        }
    }

    static void en(String val){
        def item = pickStack()
        if(item instanceof Field){
            item.setEn(val)
        }
    }

    static void cn(String val){
        def item = pickStack()
        if(item instanceof Field){
            item.setCn(val)
        }
    }

    static void converter(String val){
        def item = pickStack()
        if(item instanceof Field){
            item.setConverter(val)
        }
    }

    static void input(Closure val){
        def item = pickStack()
        if(item instanceof Converter){
            item.setInput(val)
        }
        if(item instanceof Scope){
            item.setInput(val)
        }
    }

    static void output(Closure val){
        def item = pickStack()
        if(item instanceof Converter){
            item.setOutput(val)
        }
    }

    static void input(Map val){
        def item = pickStack()
        if(item instanceof Converter){
            item.setInput(val)
        }
        if(item instanceof Scope){
            item.setInput(val)
        }
    }

    private final static Object pickStack(){
        return local.get().getLast()
    }
    private final static Object pushStack(obj){
        local.get().addLast(obj)
    }
    private final static Object popStack(){
        return local.get().removeLast()
    }
    public final static Object getElements(){
        return local.get()
    }
}